<?php

namespace App\Http\Controllers;

use App\Article;
use App\Data;
use App\Down;
use App\Earning;
use App\User;
use Carbon\Carbon;
use Illuminate\Support\Facades\Redis;

class DownController extends Controller
{
    public function downdata($id)
    {
        //查询模型所需交易币，下载地址
        $data = Data::select('id', 'coins','title','user_id','created_at','type', 'file','is_vip')->find($id);
        $userinfo = request()->user();
        $downs['user_id'] = $userinfo->id;
        $downs['data_id'] = $data->id;
        $downs['type'] = $data->type;
        $passkey = md5(time());
        if($userinfo->id == $data->user_id){
            Redis::setex('passkey'.$userinfo->id,600,$passkey);
            return response(['passkey'=>$passkey,'status'=>1,'message'=>'success']);
        }
        if($userinfo->is_vip){
            if($userinfo->vipduetime < Carbon::now()){
                $userinfo->is_vip = 0;
                $userinfo->save();
            }
        }
        if($data->is_vip && $userinfo->is_vip){
            //查询VIP下载是否存在此条下载记录 存在则不记录。
            $isdown = Down::where('user_id',$userinfo->id)->where('data_id',$data->id)->get()->toArray();
            if(empty($isdown)){
                Down::create($downs);
                $data->update(['downnum'=> $data->downnum+1]);
            }
        }else{
            //用户30天内下载的模型ID
            $userdatas = $userinfo->downs->where('created_at', '>=', Carbon::now()->modify('-30 days'))->pluck('data_id')->toArray();
            if (in_array($data->id,$userdatas)) {

            }else if ($userinfo->coins < $data->coins) {
                return response()->json(['message' => '交易币不够请充值!', 'status' => 0]);
            }else{
                $downs['coins'] = $data->coins;
                //收益金额
                $downs['money'] = $data->coins/2;
                $downs['earning_user_id'] = $data->user_id;

                //用户记录消费交易币
                $userinfo->update(['coins' => $userinfo->coins - $data->coins,'downcoins' => $userinfo->downcoins + $data->coins]);
//                if(Carbon::parse($data->created_at)->addmonth(24) > Carbon::now()){
                    $earningUser =  User::find($data->user_id);
                    //添加收益记录
                    Earning::create($downs);
                    //用户收益记录
                    $earningUser->update(['money' => $earningUser->money + $downs['money']]);
//                }else{
//                    $downs['earning_user_id'] = '';
//                    Earning::create($downs);
//                }
                //修改模型下载次数
                $data->update(['downnum'=> $data->downnum+1]);
            }
        }
        Redis::setex('passkey'.$userinfo->id,600,$passkey);
        return response(['passkey'=>$passkey,'status'=>1,'message'=>'success']);
    }

    public function downarticle($id){
        $userinfo = request()->user();
        if($userinfo->is_vip){
            if($userinfo->vipduetime < Carbon::now()){
                $userinfo->is_vip = 0;
                $userinfo->save();
            }
        }
        if($userinfo->is_vip){
            $data = Article::select('id','file')->find($id);
            $userinfo = request()->user();
            $downs['user_id'] = $userinfo->id;
            $downs['data_id'] = $data->id;
            $downs['type'] = 7;
            Down::create($downs);
            $passkey = md5(time());
            Redis::setex('passkey'.$userinfo->id,600,$passkey);
            return response(['passkey'=>$passkey,'status'=>1,'message'=>'success']);
        }else{
            return response()->json(['message' => '抱歉！ 请充值VIP!', 'status' => 0]);
        }
    }

    public function downurl($user_id,$type,$id,$passkey){
        //查询模型所需交易币，下载地址
        $repasskey = Redis::get('passkey'.$user_id);
        if($passkey != $repasskey){
            abort('404');
        }
        if($type < 6){
            $data = Data::select('id','title','file')->find($id);
        }else{
            $data = Article::select('id','title','file')->find($id);
        }
        $file = public_path('/storage/').$data->file;
        $filename = $data->title.'.rar';
        return response()->download($file,$filename);
    }

    public function downmap(){
        $file = public_path('/storage/').request('imagepath');
        $filename = substr(time(),8,16).'.jpg';
        return response()->download($file,$filename);
    }
}
